package com.swpu.service_archive.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.swpu.service_archive.entity.VaccineRecord;
import com.swpu.service_archive.mapper.VaccineRecordMapper;
import com.swpu.service_archive.service.VaccineRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chaichai
 * @since 2024-03-17
 */
@Service
public class VaccineRecordServiceImpl extends ServiceImpl<VaccineRecordMapper, VaccineRecord> implements VaccineRecordService {

    @Override
    public void saveUserVaccineData(String openid, VaccineRecord vaccineRecord) {
        vaccineRecord.setUserId(openid);
        List<String> pictureUrls = vaccineRecord.getPictureUrls();
        String pictures = "";
        if(pictureUrls.size()>0){

        }
        for (String pictureUrl : pictureUrls) {
            pictures+=pictureUrl+",";
            pictures= pictures.substring(0,pictures.length()-1);
        }
        vaccineRecord.setPictures(pictures);
        boolean isSave = this.save(vaccineRecord);
        if(!isSave){
            throw new RuntimeException("用户疫苗接种数据保存失败");
        }
    }

    @Override
    public List<VaccineRecord> getUserVaccineList(String openid) {
        LambdaQueryWrapper<VaccineRecord> wrapper = new LambdaQueryWrapper<>();
        List<VaccineRecord> vaccineList = this.list(wrapper);
        return vaccineList;
    }

    @Override
    public VaccineRecord getVaccineDataById(String id) {
        VaccineRecord vaccineRecord = this.getById(id);
        return vaccineRecord;
    }

    @Override
    public void updateUserVaccineData(VaccineRecord vaccineRecord) {
        List<String> pictureUrls = vaccineRecord.getPictureUrls();
        String pictures = "";
        if(pictureUrls.size()>0){
        for (String pictureUrl : pictureUrls) {
            pictures+=pictureUrl+",";
        }
        pictures= pictures.substring(0,pictures.length()-1);
        }
        vaccineRecord.setPictures(pictures);
        this.updateById(vaccineRecord);
    }

    @Override
    public int getVaccineRecordCount(String openid) {
        LambdaQueryWrapper<VaccineRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VaccineRecord::getUserId,openid);
        int count = this.count(queryWrapper);
        return count;
    }

    @Override
    public void deleteVaccineById(String id) {
        this.removeById(id);
    }
}
